<?php

namespace App\Admin\Controllers\Kefu;

use App\Models\Province;
use App\Models\User\User;
use App\Models\School;
use App\Models\School\Classes;
use App\Models\School\Teacher;
use App\Models\Sys;
use App\Models\Sys\Exchange;
use App\Models\Subscribe;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Row;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Column;

use App\Http\Controllers\Controller;

use Encore\Admin\Widgets;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Widgets\Collapse;
use Illuminate\Support\Facades\DB;


class ParentController extends Controller
{
    use HasResourceActions;
    protected $header = '家长列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

            $grid->model()
                ->with('vip')
                //->with('createClasses')
                //->with('students')
                ->with('subClass')
                ->with('child')
                ->where('initial_role_id',2)
                ->orderBy('id','desc');

            $grid->id('用户ID')->sortable();
            $grid->telephone('手机号');
            $grid->username('用户名');
            $grid->created_at('创建时间');

            $grid->initial_role_id('身份')->display(function ($v){
                if ($v == 1){
                    return '<span style="color:green;">老师</span>';
                } else {
                    return '<span style="color:red;">家长</span>';
                }
            });

            $grid->vip('会员到期时间')->display(function ($vip,$grid){
                if ($vip){
                    $vip = pos($vip);

                    return date('Y-m-d H:i:s',$vip['expiry_time']);
                } else {
                    return '无';
                }
            });

            /*$grid->createClasses('班级数量')->display(function ($v){
                return count($v);
            });

            $grid->students('学生人数')->display(function ($v){
                return count($v);
            });*/

            $grid->subClass('订阅班级')->display(function ($sub){
                if ($sub){
                    return count($sub);
                } else {
                    return '无';
                }
            });

            $grid->child('孩子个数')->display(function ($v){
                return count($v);
            });

            $grid->column('学校信息')->display(function (){
                $result = User::school($this->id, $this->role_id);

                $column = '';
                if ($result) {
                    foreach ($result as $key=>$val) {
                        $school_name = $val->school_name ? $val->school_name : ($val->s_name ? $val->s_name : $val->name);
                        $city = "$val->province $val->city $val->district $val->address ";
                        $column .= "$school_name  $city" . "<br>";
                    }
                }

                return $column;
            });

            $grid->customer_status('客户状态')->using(User::$customer_status_map);
            $grid->importance('重要程度')->using(User::$importance_map);


            //去除创建按钮
            $grid->disableCreateButton();

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->like('telephone','手机号');
                $filter->like('username','用户名');
                $filter->between('created_at', '创建时间')->datetime();
            });

            //操作栏
            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableDelete();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', '用户id');
            $form->display('telephone', '手机号');
            $form->select('customer_status', '客户状态')
                ->options(User::$customer_status_map);
            $form->select('importance', '重要程度')
                ->options(User::$importance_map);
            $form->textarea('note','跟进记录');


            $form->disableViewCheck();
            $form->disableEditingCheck();

        });
    }

    public function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->telephone('手机号');
        $show->username('用户名');
        $show->customer_status('客户状态');
        $show->importance('重要程度');
        $show->note(' 跟进记录');

        return $show;
    }




}
